import request from "@/utils/request";
import { IRoleListResData, IRoleBase, IConmmonRoleRes } from "./type";
import type { ICommonDataRes } from "@/types";

enum API {
  ALLROLE_URL = "/admin/acl/role/paginate",
  MARYROLE_URL = "/admin/acl/role/many",
  ROLEBASE_URL = "/admin/acl/role",
}

/**
 * 根据页码及页面显示条数获取角色列表;
 * Query参数采用=>`?page=${page}&limit=${limit}`方式
 * @param page 页码
 * @param limit 页面显示条数
 * @returns 角色列表数组
 */
export const getAllRoleApi = (page = 1, limit = 10, keyword: string) =>
  request.get<any, IRoleListResData>(
    API.ALLROLE_URL + `?page=${page}&limit=${limit}&keyword=${keyword}`
  );
// export const getMaryPermissionApi = (ids: string[]) =>
//   request.get<any, IPermissionResponsData>(
//     API.MARYPERMISSION_URL + `?ids=${ids}`
//   );
export const getRolesByidsApi = (ids: string[]) =>
  request.post<any, ICommonDataRes<IRoleBase[]>>(API.MARYROLE_URL, ids);

/**
 * 修改或增加角色API，根据参数是否包含_id判断；
 * Param参数采用=>`/${_id}`方式
 * @param data 角色数据
 * @returns
 */
export const addOrUpdateRoleApi = (data: IRoleBase) => {
  const { _id } = data;

  if (_id) {
    //防止data数据更新时，mongoDB自动更新_id
    delete data._id;
    return request.put<any, IConmmonRoleRes>(
      API.ROLEBASE_URL + `/${_id}`,
      data
    );
  } else {
    return request.post<any, IConmmonRoleRes>(API.ROLEBASE_URL, data);
  }
};

export const deleteRoleApi = (_id: string) =>
  request.delete<any, IConmmonRoleRes>(API.ROLEBASE_URL + `/${_id}`);
